<?php
/**
 * Page used to display a list of external users files
 * Also allows for uploading new files/new versions of files.
 */
// Start the session handling system
session_start();

// Connect to the database
require_once ("../db.php");

// Only allow this for external users


// select all the projects that is approved
$sql = "SELECT * FROM projects WHERE status='cleared'";
$sth = $db->prepare($sql);
$sth->execute();
$projects = $sth->fetchAll();
$group = null;              // this contains the group id for the user
$ourProjects = null;        // this contains the projectrequest for the group

if (isset($_SESSION['type']) && $_SESSION['type'] == 'student') {
    $sql = 'SELECT * FROM groupparticipants WHERE participantid="' . $_SESSION['uid'] . '"';
    $sth = $db->prepare($sql);      // if the user is a student get group id
    $sth->execute();
    $group = $sth->fetchAll();
    ?>

    <h1>Våre valgte prosjekter:</h1>
    <ol id="ourProjects">
        <?php
        if ($group) {   // if the user is in a group get all the request for that group order after prio
            $sql = 'SELECT * FROM projectrequest WHERE groupid=' . $group[0][0] . ' ORDER BY priority="low", priority="medium"';
            $sth = $db->prepare($sql);
            $sth->execute();
            $ourProjects = $sth->fetchAll();
            if (!$ourProjects) {            // if the group dont have any requests the show message
                ?> <h3>Dere har ikke valgt noen prosjekter</h3> <?php
        } else {
            for ($i = 0; $i < sizeof($ourProjects); $i++) { // loops trough the requests and displays them in a list
                foreach ($projects as $project) {
                    if ($project['id'] == $ourProjects[$i]['projectid']) {
                            ?>
                            <li class="studentlist" draggable="true" ondragstart="handleDragStart(this)" id="<?php echo $project['id']; ?>" value=" <?php echo $i + 1; ?>" >                                      
                                <img src="LnF/x_21x21.png" height="15" onclick="removeApprovedProject(<?php echo $project['id']; ?>,<?php echo $group[0][0]; ?>);">
                                <a href="javascript: ;" onclick="displayOneApprovedProject(<?php echo $project['id']; ?>)" title="<?php echo ' Eier: ' . getOwnerApproved($project['owner']); ?>" ><?php echo $project['title']; ?></a>
                            </li>       
                            <?php           // the img above have a onclick event to remove the request
                        }                   // the link above displays the project
                    }
                }
                ?>
                <script type="text/javascript">
                    var cols = document.querySelectorAll('#ourProjects .studentlist');  // This creates a drag event for all
                    [].forEach.call(cols, function(col) {                               // the requests for the group
                        col.addEventListener('dragstart', handleDragStart, false);      // change priority whit drag and drop
                        col.addEventListener('dragenter', handleDragEnter, false)
                        col.addEventListener('dragover', handleDragOver, false);
                        col.addEventListener('dragleave', handleDragLeave, false);
                        col.addEventListener('drop', handleDrop, false);
                        col.addEventListener('dragend', handleDragEnd, false);
                    });
                </script>  
                <h3>Dra prosjektene for å skifte prioritet</h3>
                <?php
            }
        } else {
            ?> <h3>Du er ikke i noen gruppe</h3> <?php      // if the user is not in a group then display message
        }?>
    </ol>
<?php } ?>
    
<h1>Alle godkjente prosjekter:</h1>
<ul id="approvedProjects">
    <?php                               // for all the users display projects that is approvedS
    foreach ($projects as $project) {
        $check = false;
        if ($ourProjects) {
            foreach ($ourProjects as $our) {
                if ($project['id'] == $our['projectid']) {  // if the request array is something then the user is in
                    $check = true;                          // a group so then if dont display the one that the group
                }                                           // have selected
            }
        }
        if ($check == false) {                              // if the group have not selected it display it and add
                                                            // onclick events to add a request and display project
            ?>

            <li class="studentlist"> <?php if ($group) { ?>
                    <img src="LnF/check_24x20.png" height="15" onclick="addApprovedProject(<?php echo $project['id']; ?>,<?php echo $group[0][0]; ?>, 'high');" >
                <?php } ?>
                <a href="javascript: ;" onclick="displayOneApprovedProject(<?php echo $project['id']; ?>)" title="<?php echo ' Eier: ' . getOwnerApproved($project['owner']); ?>" ><?php echo $project['title']; ?></a>
            </li>

            <?php
        }
    }
    ?>

</ul>

<?php
// this function returns the owner of a project
function getOwnerApproved($id) {
    require("../db.php");
    $sql = 'SELECT * FROM externalusers WHERE id="' . $id . '"';
    $sth = $db->prepare($sql);
    $sth->execute();
    $owner = $sth->fetchAll();
    return $owner[0][1];
}
?>